package com.hd.aop0410.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class NullValidateAspect {
    @Pointcut("@annotation(com.hd.aop0410.aop.NullValidate)")
    public void dataFilter() {}

    @Before("dataFilter()")
    public void before(JoinPoint pjp) throws Exception {
        log.debug("----》检查参数是否为空");
        Object[] args = pjp.getArgs();
        for (int i = 0; i < args.length; i++) {
            if (args[i]==null){
                log.error("调用方法{}时，第{}个参数为空",pjp.getSignature(),i+1);
                throw new Exception("参数为空");
            }
        }

    }
}
